<?php
namespace app\admin\controller;

use \frame\libs\BaseController;
use \app\admin\model\NodeModel;

/**
 * 登录
 */
class NodeController extends BaseController
{ 
    /**
     * 权限管理页
     * @return [type] [description]
     */
    public function index()
    {
        $this->checkPage();
        include VIEW_PATH . 'node/index.html';
    }

    /**
     * 获取所有节点
     * @return [type] [description]
     */
    public function getNodes()
    {
        echo json_encode(NodeModel::getInstance()->getNodes());
        return true;
    }

    /**
     * 获取节点数
     * @return [type] [description]
     */
    public function getNodeNum()
    {
        echo json_encode(NodeModel::getInstance()->getNodesCount());
        return true;
    }

    /**
     * 根据id获取节点
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function getNodeById()
    {
        echo $this->success(NodeModel::getInstance()->getNodeById($_POST['id']), "success", 200);
        return true;
    }

    /**
     * 更新启用状态
     * @return bool [description]
     */
    public function updateStatus()
    {
        if (!$this->checkAjax()) {
            return false;
        }
        $data = $_POST;
        $data['update_time'] = time();
        if (NodeModel::getInstance()->updateStatus($data)) {
            echo $this->success(null, "设置成功！");
            return true;
        }else{
            echo $this->error(null, "设置失败！");
            return false;
        }
    }

    /**
     * 删除
     * @return [type] [description]
     */
    public function delete()
    {
        if (!$this->checkAjax()) {
            return false;
        }
        if (NodeModel::getInstance()->deleteNodeById($_POST['id'])) {
            echo $this->success(null, "删除成功！");
            return true;
        }else{
            echo $this->error(null, "删除失败！");
            return false;
        }
    }

    /**
     * 添加节点
     * @return bool [description]
     */
    public function insert()
    {
        if (!$this->checkAjax()) {
            return false;
        }
        $data = $_POST;
        $data['create_time'] = time();
        $data['update_time'] = time();
        if (NodeModel::getInstance()->insertNode($data)) {
            echo $this->success(null, "添加成功！");
            return true;
        }else{
            echo $this->error(null, "添加失败！");
            return false;
        }
    }

    /**
     * 更新节点
     * @return bool [description]
     */
    public function update()
    {
        if (!$this->checkAjax()) {
            return false;
        }
        $data = $_POST;
        $data['update_time'] = time();
        if (NodeModel::getInstance()->updateNode($data)) {
            echo $this->success(null, "编辑成功！");
            return true;
        }else{
            echo $this->error(null, "编辑失败！");
            return false;
        }
    }

}